package com.htdata.htiiot.main;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.TimerTask;

import com.htdata.htiiot.mysql.DbConnectionPool;
import com.htdata.htiiot.mysql.JDBCUtils;
import com.htdata.htiiot.redis.ConRedis;

import redis.clients.jedis.Jedis;


public  class Task extends TimerTask {
//public  class Task {
	
	private static DbConnectionPool dbPool = DbConnectionPool.getPool();
	private static ConRedis  redis = new ConRedis();
	private Connection mysqlConn = null;
	private Jedis jedis = null;
	
	PreparedStatement psTid = null;
	ResultSet rsTid = null;
	ResultSet rsDid = null;
	ResultSet executeQuery = null;
	PreparedStatement psDid = null;
	String tidSql = "select count(*) as tidNum from tenant where deleted=0";
	String DidSql = "select count(*) as didNum from thing_device where type=0 and deleted=0";
	 int tidCount = 0;
	 int didCount = 0;
	 int i = 0;
public Task(){
		
		mysqlConn = dbPool.getConnection();
		jedis = ConRedis.getConn();
		
	}

	//@Override
	public void run() {
			// TODO Auto-generated method stub
		try {
			psTid = mysqlConn.prepareStatement(tidSql);
			rsTid = psTid.executeQuery();
			while(rsTid.next())
				tidCount  = rsTid.getInt("tidNum");
			
			psDid = mysqlConn.prepareStatement(DidSql);
			rsDid = psDid.executeQuery();
			while(rsDid.next())
			  didCount = rsDid.getInt("didNum");
			
			jedis.set("mysql:total:device",String.valueOf(didCount));  
			jedis.set("mysql:total:tenant",String.valueOf(tidCount)); 
			
	        //使用之后记得关闭连接  
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			 jedis.close(); 
			 JDBCUtils.close(psTid, rsTid, mysqlConn);
			 JDBCUtils.close(psDid, rsDid, mysqlConn);
			e.printStackTrace();
		}
		
	}
	
}
